package com.googlecode.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class TimeConverter {
	public static java.util.Date timestampToUtilDate(java.sql.Timestamp timestamp) {
		return new java.util.Date(timestamp.getTime());
	}
	
	public static java.util.Date sqlDateToUtilDate(java.sql.Date date) {
		return new java.util.Date(date.getTime());
	}

	public static java.sql.Date utilDateTosqlDate(java.util.Date date) {
		return new java.sql.Date(date.getTime());
	}
	
	public static java.sql.Timestamp utilDateToTimestamp(java.util.Date date) {
		return new java.sql.Timestamp(date.getTime());
	}
	
	public static String formatDate(java.util.Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(pattern);
		return sdf.format(date);
	}
	
	public static java.util.Date parseToDate(String date, String pattern) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(pattern);
		return sdf.parse(date);
	}
	
	/**
	 * Calendar rat tien de lay ra cac thanh phan cua thoi gian nhu ngay thang nam, gio phut giay
	 */
	public static Calendar sqlDateToCal(java.sql.Date date) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(date.getTime());
		return c;
	}
	
	public static Calendar utilDateToCal(java.util.Date date) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(date.getTime());
		return c;
	}
	
	public static Calendar timestampToCal(java.sql.Timestamp timestamp, String pattern) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(timestamp.getTime());
		return c;
	}
	
	public static void main(String args[]) {
		try {
			System.out.println(parseToDate("2012-11-11 11:11:11.123 AM", "yyyy-MM-dd hh:mm:ss.SSS aa"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
